package com.ali.ttt.mongo;
import org.bson.BsonDateTime;
import org.springframework.core.convert.converter.Converter;
import org.springframework.data.convert.WritingConverter;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;

/**
 * @Author : yuxl
 * @create 2024/7/25 2:10 PM
 */
@WritingConverter
public class LocalDateTimeToBsonDateTimeConverter implements Converter<LocalDateTime, BsonDateTime> {

    @Override
    public BsonDateTime convert(LocalDateTime source) {
        ZonedDateTime zonedDateTime = source.atZone(ZoneId.systemDefault());
        return new BsonDateTime(zonedDateTime.toEpochSecond() * 1000);
    }
}
